home *** CD-ROM | disk | FTP | other *** search
- // Copyright 1993 by Jon Dart. All Rights Reserved.
-
- #include "bookread.h"
- #include "constant.h"
- #include "bhash.h"
- #include <assert.h>
- #include <wpglob.h>
- #include <string.h>
-
- const int Entry_Size = sizeof(Book_Entry);
- const int Header_Size = 8;
- // version of opening book, change it if the format changes:
- const int Book_Version = 2;
-
- Book_Reader::Book_Reader(const char *const filename)
- {
- HRSRC hr = FindResource(App.getHinst(),filename,"BOOK");
- hBook = LoadResource(App.getHinst(),hr);
- is_open = hBook == 0 ? False : True;
- if (!is_open)
- return;
- char version;
- LPSTR lpBook = LockResource(hBook);
- version = lpBook[0];
- if (version != Book_Version)
- {
- MessageBox(NULL,"Wrong version of opening book!","",MB_OK);
- UnlockResource(hBook);
- is_open = False;
- return;
- }
- unsigned char lo_size = lpBook[1];
- unsigned char hi_size = lpBook[2];
- my_hash_size = hi_size*256 + lo_size;
- lo_size = lpBook[3];
- hi_size = lpBook[4];
- my_size = hi_size*256 + lo_size;
- UnlockResource(hBook);
- }
-
- Book_Reader::~Book_Reader()
- {
- FreeResource(hBook);
- }
-
- const unsigned Book_Reader::Head( const Board & b)
- {
- #ifdef RANGE_CHECK
- assert(is_open);
- #endif
- byte *lpBook = (byte*)LockResource(hBook);
- unsigned probe = Board_Hash::HashCode2(b) % my_hash_size;
- unsigned ret_val;
- memcpy(&ret_val,lpBook+Header_Size+2*probe,2);
- UnlockResource(hBook);
- return ret_val;
- }
-
- void Book_Reader::Fetch( const uint16 n, Book_Entry &book_entry )
- {
- #ifdef RANGE_CHECK
- assert(is_open);
- assert(n<my_size);
- #endif
- byte *lpBook = (byte*)LockResource(hBook);
- byte *entry = lpBook + Header_Size + my_hash_size*2 + n*Entry_Size;
- memcpy(&book_entry.hash_code,entry,sizeof(unsigned long));
- book_entry.recommend = entry[4];
- book_entry.move_index = entry[5];
- book_entry.next = entry[6] + entry[7]*256;
- UnlockResource(hBook);
- }
-
-
-